package nc.impl.arap.ws;

import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

import nc.bs.arap.op.ChangBxSettOP;
import nc.bs.arap.op.ChangPaySettOP;
import nc.bs.arap.op.ChangeCusSupOp;
import nc.bs.arap.op.ChangeDefdeptOp;
import nc.bs.arap.op.ChangeDeptOp;
import nc.bs.arap.op.ChangeOrgOp;
import nc.bs.arap.op.ChangePsnOp;
import nc.bs.arap.op.FileUploadOp;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.itf.arap.ws.IRQServer;
import nc.itf.uap.pf.IPFBusiAction;
import nc.vo.cmp.bill.BillAggVO;
import nc.vo.cmp.bill.BillDetailVO;
import nc.vo.pub.BusinessException;

public class RQServerImpl implements IRQServer{

	@Override
	public String chgOrg(String json) throws Exception {
		return new ChangeOrgOp().exec(json);
	}

	@Override
	public String changeDept(String json) throws Exception {
		return new ChangeDeptOp().exec(json);
	}

	@Override
	public String changeDefdept(String json) throws Exception {
		return new ChangeDefdeptOp().exec(json);
	}

	@Override
	public String changePsn(String json) throws Exception {
		return new ChangePsnOp().exec(json);
	}

	@Override
	public String changeCusSup(String json) throws Exception {
		return new ChangeCusSupOp().exec(json);
	}

	@Override
	public String custDosave__RequiresNew(JSONObject root) throws Exception {
		return new ChangeCusSupOp().doSave_Single(root);
	}

	@Override
	public String custDoassign__RequiresNew(JSONObject root, String voPk) throws Exception {
		return new ChangeCusSupOp().doAssign_Single(root, voPk);
	}
	
	@Override
	public String saveOAPaySett(String json) throws Exception {
		return new ChangPaySettOP().synSett(json);
	}

	@Override
	public String saveTMSBxSett(String json) throws Exception {
		return new ChangBxSettOP().synSett(json);
	}

	@Override
	public String fileUpload(String json) throws Exception {
		return new FileUploadOp().exec(json);
	}

	@Override
	public String hxdbCust(String json) throws Exception {
		return new nc.bs.arap.op.HxdbCustOp().exec(json);
	}

	@Override
	public void saveBillVO_RequiresNew(List<BillAggVO> list) throws BusinessException {
		InvocationInfoProxy.getInstance().setGroupId(list.get(0).getParentVO().getAttributeValue("pk_group")+"");
		InvocationInfoProxy.getInstance().setUserId(list.get(0).getParentVO().getAttributeValue("creator")+"");
		HashMap<String, Object> eParam = new HashMap<String, Object>();
		eParam.put("notechecked", "notechecked");
		IPFBusiAction iba = NCLocator.getInstance().lookup(IPFBusiAction.class);
		for(int i=0;i<list.size();i++) {
			BillAggVO aggvo=list.get(i);
			aggvo.getParentVO().setPrimaryKey(null);
			aggvo.getParentVO().setAttributeValue("bill_no", null);
			BillDetailVO[] childrenVO = (BillDetailVO[]) aggvo.getChildrenVO();
			for(BillDetailVO bvo:childrenVO) { 
				bvo.setPrimaryKey(null);
			}
			BillAggVO[] aggvos = { aggvo };
			iba.processAction("SAVEBASE", "F5", null, aggvo, aggvos, eParam);
		}
	}

	@Override
	public void updateSql_RequiresNew(String sql) throws DAOException {
		BaseDAO dao=new BaseDAO();
		dao.executeUpdate(sql);
	}
}
